﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.Roles;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.Roles
{
    public class UpdRoleStateCommandHandler : IRequestHandler<UpdRoleStateCommand, APIResult<int>>
    {
        IBaseRepository<Role> rRep;
        ILogger<UpdRoleStateCommandHandler> logger;

        public UpdRoleStateCommandHandler(IBaseRepository<Role> rRep, ILogger<UpdRoleStateCommandHandler> logger)
        {
            this.rRep = rRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(UpdRoleStateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var rolelist = await rRep.GetAsync(x => x.RoleId == request.RoleId);
                if (rolelist == null) 
                {
                    return new APIResult<int>
                    {
                        Code=StatusEnumCode.Fail,
                        Message="您要设置的角色不存在",
                        Data=0
                    };
                }
                rolelist.RoleStatus=request.RoleStatus;
                await rRep.UpdAsync(rolelist);
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "设置角色状态成功",
                    Data = 1
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
